package com.ruoyi.thoroughfare.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.thoroughfare.domain.bo.AppMerchantBo;
import com.ruoyi.thoroughfare.domain.vo.AppMerchantVo;
import com.ruoyi.thoroughfare.domain.AppMerchant;
import com.ruoyi.thoroughfare.mapper.AppMerchantMapper;
import com.ruoyi.thoroughfare.service.IAppMerchantService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商户管理Service业务层处理
 *
 * @author Zach
 * @date 2023-11-14
 */
@RequiredArgsConstructor
@Service
public class AppMerchantServiceImpl implements IAppMerchantService {

    private final AppMerchantMapper baseMapper;

    /**
     * 查询商户管理
     */
    @Override
    public AppMerchantVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商户管理列表
     */
    @Override
    public TableDataInfo<AppMerchantVo> queryPageList(AppMerchantBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppMerchant> lqw = buildQueryWrapper(bo);
        Page<AppMerchantVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商户管理列表
     */
    @Override
    public List<AppMerchantVo> queryList(AppMerchantBo bo) {
        LambdaQueryWrapper<AppMerchant> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppMerchant> buildQueryWrapper(AppMerchantBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppMerchant> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), AppMerchant::getName, bo.getName());
        lqw.eq(bo.getUserId() != null, AppMerchant::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getHeadPhoto()), AppMerchant::getHeadPhoto, bo.getHeadPhoto());
        lqw.eq(StringUtils.isNotBlank(bo.getStorePhoto()), AppMerchant::getStorePhoto, bo.getStorePhoto());
        lqw.eq(StringUtils.isNotBlank(bo.getCashierPhoto()), AppMerchant::getCashierPhoto, bo.getCashierPhoto());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessLicense()), AppMerchant::getBusinessLicense, bo.getBusinessLicense());
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyCode()), AppMerchant::getCompanyCode, bo.getCompanyCode());
        lqw.like(StringUtils.isNotBlank(bo.getCompanyName()), AppMerchant::getCompanyName, bo.getCompanyName());
        return lqw;
    }

    /**
     * 新增商户管理
     */
    @Override
    public Boolean insertByBo(AppMerchantBo bo) {
        AppMerchant add = BeanUtil.toBean(bo, AppMerchant.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商户管理
     */
    @Override
    public Boolean updateByBo(AppMerchantBo bo) {
        AppMerchant update = BeanUtil.toBean(bo, AppMerchant.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppMerchant entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商户管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
